<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>title</title>
  <!--按顺序引入-->
  <script src="https://cdn.staticfile.org/react/16.8.4/umd/react.development.js"></script>
  <script src="https://cdn.staticfile.org/react-dom/16.8.4/umd/react-dom.development.js"></script>
  <script src="https://cdn.staticfile.org/babel-standalone/6.26.0/babel.min.js"></script>
  <script src="../js/babel.min.js"></script>
  <script src="https://cdn.bootcdn.net/ajax/libs/prop-types/15.8.1/prop-types.js"></script>
  <style>
    body {
      width: 95vw;
      height: 80vh;
      background-color: #2b2b2b;
      color: wheat;
    }
  </style>
</head>
<body>
<div id="app"></div>

<script type="text/babel">
  class Demo extends React.Component {
    state = {opacity: 1}

    // 生命周期 组件挂载完毕
    componentDidMount() {
      this.timer = setInterval(() => {
        let {opacity} = this.state
        opacity -= 0.2
        if (opacity <= 0)
          opacity = 1
        this.setState({opacity})
      }, 200)
    }

    // 组件将卸载时
    componentWillUnmount() {
      clearInterval(this.timer)
    }

    // 初始化
    render() {
      return (
          <div>
            <h1 style={{opacity: this.state.opacity}}>REACT学不会怎么办</h1>
            <button onClick={this.disappear}>不活了,嘿嘿</button>
          </div>
      )
    }

    disappear = () => {
      ReactDOM.unmountComponentAtNode(document.getElementById('app'))
    }
  }

  ReactDOM.render(<Demo/>, document.getElementById('app'))
</script>
</body>
</html>
